package com.hjh.springboot_jpademo;

import com.hjh.springboot_jpademo.Repository.ProviderRepository;
import com.hjh.springboot_jpademo.Service.ProviderService;
import com.hjh.springboot_jpademo.entity.Provider;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import javax.annotation.Resource;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ProviderRepositoryTester {

    @Resource
    private ProviderService providerService;

    @Test
    public void testInsert(){
        Provider provider = new Provider();
        provider.setProCode("test2");
        provider.setProName("test2");
        provider.setProContact("test2");
        provider.setProDesc("test2");
        providerService.save(provider);
    }

    @Test
    public void getAll(){
        Iterable<Provider> iterable = providerService.getAll();
        iterable.forEach(provider -> System.out.println(provider.getProName()));
    }

    @Test
    public void getById(){
        Provider provider = providerService.getById(1);
        System.out.println("proName:"+provider.getProName());
    }

    @Test
    public void testUpdate(){
//        Provider provider = new Provider();
//        provider.setId(28);
        Provider provider = providerService.getById(28);
        providerService.update(provider);

    }

    @Test
    public void testDelete(){
        providerService.delete(27);
    }

    @Test
    public void testfindByproCodeAndproName(){
        List<Provider> list = providerService.findByProCodeAndAndProName("GZ_GYS003","广州市白云区美星五金制品厂");
        list.forEach(provider -> {
            System.out.println("供应商编码:"+provider.getProCode());
            System.out.println("供应商名称:"+provider.getProName());
            System.out.println("供应商详细描述:"+provider.getProDesc());
        });
    }

    @Test
    public void testGetByNameLike(){
        List<Provider> list = providerService.findByProCodeLike("SD_GYS001");
        list.forEach(provider -> {
            System.out.println("供应商编码:"+provider.getProCode());
            System.out.println("供应商名称:"+provider.getProName());
            System.out.println("供应商详细描述:"+provider.getProDesc());
        });
    }

    @Test
    public void testfindAllSort(){
        Sort sort = new Sort(Sort.Direction.DESC,"id");
        Iterable<Provider> iterable = providerService.findAllSort(sort);
        iterable.forEach(provider -> System.out.println("proCode "+provider.getProCode()+" --- proName = " + provider.getProName() + "--- proDesc ="+provider.getProDesc()));
    }

    @Test
    public void testFindAllPage(){
        Sort sort = new Sort(Sort.Direction.DESC,"id");
        Pageable pageable = PageRequest.of(1,2,sort); //参数：page 页码， 从 0 开始
        Page<Provider> providerPage = providerService.findAllPage(pageable);
        System.out.println("总页数："+providerPage.getTotalPages());
        System.out.println("总记录数："+providerPage.getTotalElements());
        System.out.println("当前页码："+(providerPage.getNumber()+1));
        System.out.println("当前页面记录数："+providerPage.getNumberOfElements());

        List<Provider> list = providerPage.getContent();
        list.forEach(provider -> System.out.println("proCode "+provider.getProCode()+" --- proName = " + provider.getProName() + "--- proDesc ="+provider.getProDesc()));
    }
}
